<?php

namespace App\Http\Controllers;

use App\Http\Model\WxFormIdData;
use App\Http\Utils\Common\OperResult;
use Carbon\Carbon;
use Illuminate\Http\Request;

use Log;
use Exception;

class FormIdController extends Controller
{

    public function uploadFormid(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            Log::info($request->all());
            if (!$request->has('formid') || !$request->has('account_id') || !$request->has('account_type') || !$request->has('own_id')) {
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => array()]);
            }
            Log::info('====uploadFormid======');
            WxFormIdData::updateOrCreate(['formid'=>$request->formid],[
                'formid' => $request->formid,
                'account_id' => $request->account_id,
                'account_type' => $request->account_type,
                'own_id' => $request->own_id,
                'over_date'=> Carbon::now()->addDays(7),
                'openid'=>$request->openid
            ]);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => []]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => array()]);
        }
    }


}
